package com.banciyuan.bcywebview.biz.detail.noveldetail.holder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.tag.TagView;
import com.banciyuan.bcywebview.base.view.textview.LetterSpaceTextView;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.regex.MatchFilterImpl;
import com.banciyuan.bcywebview.utils.regex.MyLinkfy;
import com.banciyuan.bcywebview.utils.regex.MyRegex;
import com.banciyuan.bcywebview.utils.string.UnderLineUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.Role;
import de.greenrobot.daoexample.model.TagDetail;

/**
 * Created by squall on 2016/6/7.
 */
public class TitleHolder extends RecyclerView.ViewHolder {
    private TextView title_tv;
    private LetterSpaceTextView work_tv;
    private View zuopin_view;
    private TagView roles_tg;
    private View roles_view;
    private TagView tags_fv;
    private View tags_view;
    private TextView intro_tv;
    private View viewIntroContainer;

    public TitleHolder(View view) {
        super(view);
        title_tv = (TextView) view.findViewById(R.id.tv_work_title);
        intro_tv = (TextView) view.findViewById(R.id.tv_work_intro);
        work_tv = (LetterSpaceTextView) view.findViewById(R.id.tv_works);
        zuopin_view = view.findViewById(R.id.tv_zuopin);
        roles_tg = (TagView) view.findViewById(R.id.fv_role);
        roles_view = view.findViewById(R.id.tv_juese);
        tags_fv = (TagView) view.findViewById(R.id.fv_tag);
        tags_view = view.findViewById(R.id.tv_tag);
        viewIntroContainer = view.findViewById(R.id.novel_intro_container);
    }

    public void setData(Context mContext, Complex novel) {
        setWork(mContext, novel);
        setTitleAndIntro(mContext, novel);
        setTag(mContext, novel.getPost_tags());
        setRole(novel);
        registerForContentMenu(mContext);
    }

    private void setRole(Complex novel) {
        if (novel.getProperties() != null) {
            List<Role> character = novel.getProperties().getCharacter();
            List<TagDetail> tags = new ArrayList<>();
            for (int i = 0; i < character.size(); i++) {
                TagDetail tagDetail = new TagDetail();
                tagDetail.setTag_name(character.get(i).getRole_name());
                tags.add(tagDetail);
            }
            if (character.size() > 0) {
                roles_tg.setVisibility(View.VISIBLE);
                roles_view.setVisibility(View.VISIBLE);
                roles_tg.setTlist(tags);
            } else {
                roles_tg.setVisibility(View.GONE);
                roles_view.setVisibility(View.GONE);
            }
        }

    }

    public void setTag(final Context mContext, List<TagDetail> tags) {
        if (null != tags && tags.size() > 0) {
            tags_view.setVisibility(View.VISIBLE);
            tags_fv.setVisibility(View.VISIBLE);
            tags_fv.setTlist(tags);
            tags_fv.setTagViewClick(new TagView.OnTagClickListener() {
                @Override
                public void onClick(TagDetail t) {
                    if (TextUtils.isEmpty(t.getEvent_id())) {
                        Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                        intent.putExtra("type", "tag");
                        mContext.startActivity(intent);
                    } else {
                        gotoUtil.gotoAct(mContext, EventDetailActivity.class, t.getEvent_id());
                    }
                }
            });
        } else {
            tags_view.setVisibility(View.GONE);
            tags_fv.setVisibility(View.GONE);
        }
    }

    private void setTitleAndIntro(Context mContext, Complex novel) {
        String title = novel.getTitle();
        if (!TextUtils.isEmpty(title)) {
            title = title.replace("&amp;", "&");
            title_tv.setText(Html.fromHtml(title));
        }
        String intro = novel.getIntro();
        if (TextUtils.isEmpty(intro)) {
            viewIntroContainer.setVisibility(View.GONE);
        } else {
            intro_tv.setText(Html.fromHtml(novel.getIntro()));
            MyLinkfy.addLinks(intro_tv, MyRegex.plist.get(0), "bcyapp_", new MatchFilterImpl(), null);
            UnderLineUtil.DisUnderLine(mContext, intro_tv);
        }
    }

    private void setWork(final Context mContext, final Complex novel) {
        if (!TextUtils.isEmpty(novel.getWork())) {
            work_tv.setVisibility(View.VISIBLE);
            zuopin_view.setVisibility(View.VISIBLE);
            work_tv.setSpacing(1.2f);
            work_tv.setText(novel.getWork());
            work_tv.setTextColor(TextUtils.isEmpty(novel.getWid()) ?
                    mContext.getResources().getColor(R.color.grey_ten_level) : mContext.getResources().getColor(R.color.timeline_green));
        } else {
            work_tv.setVisibility(View.GONE);
            zuopin_view.setVisibility(View.GONE);
        }
        work_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(novel.getWid()))
                    gotoUtil.gotoAct(mContext, CircleSmoothActivity.class, novel.getWork(), novel.getWid());
            }
        });
    }


    private void registerForContentMenu(Context mContext) {
        ((Activity) mContext).registerForContextMenu(intro_tv);
        ((Activity) mContext).registerForContextMenu(title_tv);
    }

    public void unRegisterForContentMenu(Context mContext) {
        ((Activity) mContext).unregisterForContextMenu(intro_tv);
        ((Activity) mContext).unregisterForContextMenu(title_tv);
    }
}
